package com.gxa.meiye.portal.controller.zy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.domain.Consumer;
import com.gxa.meiye.core.pojo.domain.Order;

import com.gxa.meiye.portal.hread.UserThreadLocal;
import com.gxa.meiye.portal.service.zy.OrderService;
import com.gxa.meiye.portal.utils.PayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

/**
 * @Author: zy
 * @CreateTime: 2022-07-21  15:29
 */
@RestController
@Api(value = "创建订单",tags = "创建订单接口")
@RequestMapping("/ordercha")
public class OrderChaController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PayUtil payUtil;
//    //立即预约
//    @PostMapping("/coupon")
//    public Map orderCoupon(Order order) throws JsonProcessingException {
//        //从本地线程池获取当前消费者电话
//        Object o = UserThreadLocal.get();
//        ObjectMapper objectMapper=new ObjectMapper();
//        Consumer consumer = objectMapper.readValue(o.toString(), Consumer.class);
//        String phone = consumer.getPhone();
//        boolean b = orderService.orderCoupon(phone, order);
//        JsonResult jsonResult=new JsonResult();
//        if (b){
//            jsonResult.setCode("1");
//            jsonResult.setMsg("预约成功");
//        }
//        jsonResult.setCode("0");
//        jsonResult.setMsg("预约失败");
//        return jsonResult.getMap();
//    }

    //创建订单
    @ApiOperation(value = "创建订单")
    @PostMapping("/order")
    public Map orderAdd( Order order) throws JsonProcessingException {
        System.out.println(order);
    //从本地线程池获取当前消费者电话
    Object o = UserThreadLocal.get();
    ObjectMapper objectMapper=new ObjectMapper();
    Consumer consumer = objectMapper.readValue(o.toString(), Consumer.class);
    String phone = consumer.getPhone();
        //String phone=null;
        //订单编号
        String s = "meiye"+ UUID.randomUUID().toString().replaceAll("-", "");
        order.setOrderNumber(s);
        boolean b = orderService.orderAdd(phone, order);
        JsonResult jsonResult=new JsonResult();
        //流水号

        String liuSui = UUID.randomUUID().toString().replaceAll("-", "");
//        String pay = payUtil.pay(liuSui,order.getGetMoney().toString());
//
//
//        if (!StringUtils.isEmpty(pay)){
//            boolean update = orderService.update(s,pay);
//            jsonResult.setCode("1");
//            if (update) {
//
//                jsonResult.setMsg("付款成功,已支付订单");
//            }else {
//                jsonResult.setMsg("状态修改失败");
//
//            }
//            return jsonResult.getMap();
//        }
        jsonResult.setCode("2");
        jsonResult.setMsg("创建成功");
        return jsonResult.getMap();
    }
}
